import React, { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useNavigate, Outlet } from 'react-router-dom';

const AuthGuard = () => {
  const token = useSelector((state) => state.auth.token);
  const navigate = useNavigate();

  useEffect(() => {
    if (!token) {
      navigate('/login');
    }
  }, [token, navigate]);

  // If there's no token, don't render anything (we're redirecting)
  if (!token) {
    return null;
  }

  // If there is a token, render the child routes
  return <Outlet />;
};

export default AuthGuard;